/**
 * Copyright (c) 2011 Tomi Paananen.
 *
 * Part of Quick Gestures engine library.
 */

#ifndef LOGGER_H
#define LOGGER_H

#include <QFile>
#include <QString>
#include <QTime>


/*!
  \class Logger
  \brief Used for debugging and analyzing the application behavior.
*/


class Logger
{
public:

    /*!
      Sets the name of the log file as \a filename.
      Returns true if the file is valid, false otherwise.
    */
    static bool setLogFile(const QString &filename)
    {
        // Test if the file is valid and can be written.
        if (QFile::exists(filename) &&
            m_logFile.open(filename, QFile::WriteOnly)) {
            m_logFilename = filename;
            m_logFile.close();
            return true;
        }

        return false;
    }

    /*!
      Logs \a text into the log file.
      Returns true if successful, false otherwise.
    */
    static bool log(const QString &text)
    {
        if (m_logFile.open(m_logFilename, QFile::Append | QFile::WriteOnly)) {
            m_logFile << text << "\n";
            m_logFile.close();
            return true;
        }

        return false;
    }

    /*!
      Starts timing.
    */
    static void startTiming()
    {
        m_timer.restart();
    }

    /*!
      Returns the number of milliseconds elapsed since startTiming() was
      called.
    */
    static int stopTiming()
    {
        return m_timer.elapsed();
    }

private: // Data
    static QFile m_logFile;
    static QString m_logFilename;
    static QTime m_timer;
};


#endif // LOGGER_H

// End of file.
